---
title: "Figure 3: counterfactual graph"
output:
  pdf_document:
    keep_tex: true
date: "2023"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(haven)
library(dplyr)
library(ggplot2)

### find dropbox


get_hostname <- function(){ 
  return(as.character(Sys.info()["nodename"])) 
} 

print(get_hostname())

ets_Counter=read_dta(paste0("Forcounterfactual.dta" )) 


makepf=function(cfvar="coal_postCFBadtoGood"){
  pf= ets_Counter %>%   mutate(period=1,value=1,type="actual")
  pf=pf %>% bind_rows( ets_Counter %>%   mutate(period=2,value=coal_postETS/coal_preETS*1,type="actual") )
  pf=pf %>% bind_rows( ets_Counter %>%   mutate(period=1,value=coal_preETS/coal_preETS*1,type="cf") )
  pf=pf %>% bind_rows( ets_Counter %>%   mutate(period=2,value=!!sym(cfvar) /coal_preETS*1,type="cf")) 
  pf=pf %>%   rename(cf=value,lab=type)
  
  return(pf)
} 

  
pf=makepf()



```






```{r counterfactuals}
#pf["g"]=0  

plotpf=function(pf){
pp=ggplot(pf, aes(x=period,ymax=cf*100,fill=lab,ymin=0,color=lab))+
  scale_fill_grey( name = "Scenario" ,label=c("Actual","Good quality Management only"))+
  scale_color_grey( name = "Scenario",label=c("Actual","Good quality Management only"))+
  geom_ribbon( alpha=0.5) + xlab("") + ylab("% of pre ETS level")+
  scale_x_discrete(labels=c("1" = "Pre ETS", "2" = "Post ETS"))+
  annotate("text", x = 1, y = -5, label = "Pre ETS")+
  annotate("text", x = 2, y = -5, label = "Post ETS") +theme(axis.text=element_text(size=14),legend.text=element_text(size=11) ) 
}

pp=plotpf(pf)
pp


ggsave( paste0("Figure3.pdf") ,width=19, units="cm",  plot = pp)


```



